<template>
  <div class="">
    <el-dialog class="m_dialog" :title="title" :visible.sync="dialogVisible" width="800" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <div class="content">
        <custom-form ref="FireFightingForm" formRef="add" :formModel="formModel" :formItemConfig="formItemConfig" :formRules="formRules" label-width="80px" label-position="left" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="btn_cancel">取 消</el-button>
        <el-button type="primary" @click="submitFrom('FireFightingForm')" class="btn_primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { addiFreControlPoint, updateFireControlPoint, getProjectPointControlList, delFireControlPoint, getFireControlPoint } from "@/api"
export default {
  // props:["dialogVisible"],
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    formData: {
      type: Object,
      default: () => { },
    },
    title: {
      type: String,
      default: () => { },
    },
    // titleIndex: {
    //   type: Number | String,
    //   default: 0,
    // },
    // descData: {
    //   type: Object,
    //   default: () => {},
    // },
    dictMap: {
      type: Object,
      default: () => { },
    },
  },
  data () {
    return {
      formModel: {
        fireControlPointName: '',
        fireControlPointType: '',
        fireControlPointId: undefined,
        lat: '',
        lng: '',
        projectPointId: '',
        projectPointTypeId: '',
        deptName: "",
        remark: '',
      },
      // 表单配置项
      formItemConfig: [
        {
          label: '所属项目',
          prop: 'deptName',
          span: 24,
          disabled: true,
        },
        {
          label: '类型',
          prop: 'fireControlPointType',
          component: 'BaseRadioGroup',
          span: 24,
          options: [],
        },
        {
          label: '名称',
          prop: 'fireControlPointName',
          span: 24,
          clearable: true,
        },
        {
          label: '经度',
          prop: 'lng',
          span: 24,
          clearable: true,
        },
        {
          label: '纬度',
          prop: 'lat',
          span: 24,
          clearable: true,
        },
        {
          label: '备注',
          prop: 'remark',
          span: 24,
          clearable: true,
        },

      ],
      // 表单验证
      formRules: {
        deptName: {
          required: true,
          message: '所属项目不能为空',
          trigger: 'blur',
        },
        fireControlPointType: {
          required: true,
          message: '类型不能为空',
          trigger: 'blur',
        },
        fireControlPointName: {
          required: true,
          message: '名称不能为空',
          trigger: 'blur',
        },
        lng: {
          required: true,
          message: '经度不能为空',
          trigger: 'blur',
        },
        lat: {
          required: true,
          message: '纬度不能为空',
          trigger: 'blur',
        },
      },
    };
  },
  computed: {
  },
  watch: {
    formData: {
      deep: true,
      immediate: true,
      handler () {
        this.formModel = JSON.parse(JSON.stringify(this.formData));
        this.setDictList();
      },
    },
  },
  created () {
    this.setDictList();
  },
  mounted () { },
  methods: {

    cancel () {
      this.$emit('cancel');
    },
    getList () {
      this.$emit('getList');
    },
    // 设置formItemConfig里面的字典列表
    setDictList () {
      if (this.dictMap) {
        // 操作类型
        this.formItemConfig[this.formItemConfig.findIndex(item => item.prop == 'fireControlPointType')].options = this.dictMap.type.fire_control_point_type;
        // 操作状态
        // this.formItemConfig[this.formItemConfig.findIndex(item => item.prop == 'status')].options = this.dictMap.type.sys_common_status;
      }
    },
    // 提交表单判断是添加还是修改
    submitFrom (form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.formModel.fireControlPointId !== undefined) {
            updateFireControlPoint(this.formModel).then(response => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.cancel()
              this.getList()
            })
          } else {
            let temData = {}
            temData.projectPointName = this.formData.deptName
            temData.deptId = this.formData.deptId
            getProjectPointControlList(temData).then(res => {
              if (res.rows[0]) {
                this.formModel.projectPointId = res.rows[0].projectPointId
                // this.formModel.projectPointTypeId = res.rows[0].projectPointTypeId
                this.formModel.projectPointTypeId = '666'
                addiFreControlPoint(this.formModel).then(resp => {
                  if (resp.code === 200) {
                    this.$message({
                      message: "添加成功",
                      type: "success",
                    });
                  } else {
                    this.$message({
                      message: resp.msg,
                      type: "error",
                    });
                  }
                  this.cancel()
                  this.getList()
                })
              }
            })

          }
        }
      })
    },
  },
};
</script>
  
<style lang="less" scoped></style>
  